package com.finance.util;


import com.finance.system.bean.pojo.Menu;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * Created by CBWL on 2017/3/17.
 */
public class MenuUtil {


    public static Menu fillMenuTree(Collection<Menu> menuCollection){
        Map<String,Menu>menuMap=new HashMap<String,Menu>();
        for (Menu menu:menuCollection){
            if(StringUtils.isNotEmpty(menu.getCode())) {
                menuMap.put(menu.getCode(), menu);
            }
        }
        Menu root = new Menu();
        Iterator<Map.Entry<String,Menu>> iterator=menuMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Menu> entry=iterator.next();
            Menu menu=entry.getValue();
            String pCode=menu.getPCode();
            Menu pMenu=menuMap.get(pCode);
            if(pMenu!=null){
                pMenu.append(menu);
            }else{
                root.append(menu);
            }
        }
        return root;
    }

    public static Menu MenuTree(Collection<Menu> menuCollection){
        Map<String,Menu>menuMap=new HashMap<String,Menu>();
        for (Menu menu:menuCollection){
            if(StringUtils.isNotEmpty(menu.getCode())) {
                menuMap.put(menu.getCode(), menu);
            }
        }
        Menu root = new Menu();
        Iterator<Menu> iterator= menuCollection.iterator();
        while (iterator.hasNext()){
            Menu menu=iterator.next();
            String pCode=menu.getPCode();
            Menu pMenu=menuMap.get(pCode);
            if(pMenu!=null){
                pMenu.append(menu);
            }else{
                root.append(menu);
            }
        }
        return root;
    }
}
